/*
 * @Descripttion: 老师模块控制器
 * @version: 
 * @Author: hzx
 * @Date: 2021-03-06 15:10:44
 * @LastEditors: hzx
 * @LastEditTime: 2021-05-14 16:11:07
 */

'use strict';

const Controller = require('egg').Controller;

class TeacherController extends Controller {
    async index() {
        this.ctx.body = "api hi"
    }

    /**
     * @name: 获取所有老师信息
     * @msg: '老师信息查询成功'
     * @param { object } ctx
     * @return {*}
     */
    async getTeachers() {
        let sql = "select *,FROM_UNIXTIME(rx_time,'%Y-%m-%d') as rx_time from teacher";
        const results = await this.app.mysql.query(sql);
        //console.log(sql);
        this.ctx.body = {
            code: '200',
            msg: '老师信息查询成功',
            data: results,
            result: true
        }
    }

    /**
     * @name: 根据学生id查询学生信息
     * @msg: '学生信息查询成功'
     * @param { t_id }
     * @return {*}
     */
    async selectTeacherId() {
        let { t_id } = this.ctx.params;
        const sql = `select *,FROM_UNIXTIME(rx_time,'%Y-%m-%d') as rx_time from teacher where t_id=${t_id}`;
        //console.log(sql);
        let results = await this.app.mysql.query(sql);
        this.ctx.body = {
            code: '200',
            msg: '老师信息查询成功',
            data: results,
            result: true
        }
    }

    /**
     * @name: 查询班级id查询班级所有学生
     * @msg: '查询成功'
     * @param { class_id }
     * @return {*}
     */

    async selectClassId() {
        let { class_id } = this.ctx.params;
        const sql = `select * from student where class_id=${class_id}`;
        //console.log(sql);
        let results = await this.app.mysql.query(sql);
        this.ctx.body = {
            code: '200',
            msg: '查询成功',
            data: results,
            result: true
        }
    }

    /**
     * @name: 增加老师信息
     * @msg:  '插入成功'
     * @param {*} t_id, t_name, t_sex, ddev_id ,rx_time必填
     * @return {*}
     */
    async insterTeacher() {
        let { t_id, t_name, dev_id, t_sex, rx_time } = this.ctx.request.body;
        //console.log(this.ctx.request.body)
        let sql = `insert into teacher( t_id, t_name, dev_id, t_sex, rx_time) values('${t_id}', '${t_name}', '${dev_id}', '${t_sex}', ${rx_time})`
        //console.log(sql)
        const result = await this.app.mysql.query(sql);
        const insertSuccess = result.affectedRows === 1;
        const insertId = t_id;
        //console.log(insertSuccess);
        this.ctx.body = {
            isSuccess: insertSuccess,
            insertId: insertId,
            code: '200',
            msg: '插入成功',
            result: true
        }
    }

    /**
     * @name: 修改老师信息
     * @msg:  '修改成功'
     * @param {*}  t_id, t_name, t_sex, ddev_id ,rx_time 选填
     * @return {*}
     */
    async updateTeacher() {
        let { t_id, t_name, t_sex, dev_id, rx_time } = this.ctx.request.body;
        let sql = `update teacher set t_id = '${t_id}', t_name = '${t_name}', t_sex = ${t_sex}, dev_id = '${dev_id}', rx_time = ${rx_time} where t_id = '${t_id}'`
        //console.log(sql)
        const result = await this.app.mysql.query(sql);
        const updateSuccess = result.affectedRows === 1;
        //console.log(updateSuccess)
        this.ctx.body = {
            isSuccess: updateSuccess,
            code: '200',
            msg: '修改成功',
            result: true
        }
    }


    /**
     * @name: 根据id删除信息
     * @msg: 
     * @param {*} t_id
     * @return {*}
     */
    async delTeacher() {
        let t_id = this.ctx.params.t_id;
        //console.log(t_id)
        const res = await this.app.mysql.delete('teacher', { 't_id': t_id });
        //console.log(res)
        this.ctx.body = {
            code: '200',
            data: res,
            msg: '删除成功',
            result: true
        }
    }

    /**
     * @name: 根据部门名称查询老师信息
     * @msg:  部门信息查询成功
     * @param {*} dev_name
     * @return {*}
     */
    async selectDevname() {
        let { dev_name } = this.ctx.query;
        let sql = `select * from teacher left join department on teacher.dev_id = department.dev_id where department.dev_name='${dev_name}'`;
        const results = await this.app.mysql.query(sql);
        this.ctx.body = { 
            code: '200',
            msg: '查询成功',
            data: results,
            result: true
        } 
    }
}

module.exports = TeacherController
